{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "一个简单的线性神经网络-1",
   "id": "2792be06407627b1"
  },
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "import torch\n",
    "import numpy as np\n",
    "xy=np.loadtxt('../data/diabetes.csv.gz',delimiter=',',dtype=np.float32)\n",
    "x_data=torch.from_numpy(xy[:,:-1])\n",
    "y_data=torch.from_numpy(xy[:,[-1]])\n",
    "print(\"features shape:\",x_data.shape)\n",
    "print(\"labels shape:\",y_data.shape)\n",
    "print(\"feature的前五个是：\\n\",x_data[0:5,:])\n",
    "print(\"label的前五个是：\\n\",y_data[0:5])"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "class MyModel(torch.nn.Module):\n",
    "    def __init__(self):\n",
    "        super(MyModel, self).__init__()\n",
    "        self.linear1=torch.nn.Linear(8,6)\n",
    "        self.linear2=torch.nn.Linear(6,4)\n",
    "        self.linear3=torch.nn.Linear(4,1)\n",
    "        self.sigmoid=torch.nn.Sigmoid()\n",
    "    def forward(self,x):\n",
    "        x=self.sigmoid(self.linear1(x))\n",
    "        x=self.sigmoid(self.linear2(x))\n",
    "        x=self.sigmoid(self.linear3(x))\n",
    "        return x"
   ],
   "id": "7ce9ae946cada8b8",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": "model=MyModel()",
   "id": "475eaaa62f0c7e9f",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# criterion=torch.nn.BCELoss(size_average=True)\n",
    "# UserWarning: 'size_average' and reduce args will be deprecated, please use reduction='mean' instead.\n",
    "criterion=torch.nn.BCELoss(reduction='mean')\n",
    "optimizer=torch.optim.SGD(model.parameters(),lr=0.01)"
   ],
   "id": "63fcdbe6ea625a3e",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "for epoch in range(100):\n",
    "    #Forward\n",
    "    y_pred=model(x_data)\n",
    "    loss=criterion(y_pred,y_data)\n",
    "    print(\"epoch:\",epoch,\"loss:\",loss.item())\n",
    "    #Backward\n",
    "    optimizer.zero_grad()\n",
    "    loss.backward()\n",
    "    #Update\n",
    "    optimizer.step()"
   ],
   "id": "a2b9f0defc8a35d9",
   "outputs": [],
   "execution_count": null
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
